layui.define(['jquery', 'kaBgUrl'],
    function (exports) {
        var $ = layui.jquery;
        var kaBgUrl = layui.kaBgUrl;
        var kaLanguage = new function () {
            this.forDomx = function (root1) {//主程序加载 替换中文
                var clv = kaValue.clv();
                this.forDomTypex("placeholder", root1, clv);
                this.forDomTypex("title", root1, clv);
                this.forDomTypex("value", root1, clv);
                if (root1.children != undefined && root1.children.length > 0) {
                    var root1Text = $(root1).contents().filter(function () { return this.nodeType === 3; }).text();
                    // var root1TextJson = sessionStorage.getItem("root1Text");
                    // if (root1TextJson == undefined || root1TextJson == null || root1TextJson == "" || root1TextJson == "{}") {
                    //     root1TextJson = {}
                    // } else {
                    //     root1TextJson = JSON.parse(root1TextJson);
                    // }
                    // root1TextJson[root1Text] = "";
                    // sessionStorage.setItem("root1Text", JSON.stringify(root1TextJson));
                    var html = ``;
                    for (var i = 0; root1.children[i] != undefined; i++) {
                        html = html + $(root1.children[i]).prop("outerHTML");
                        this.forDomx(root1.children[i]);
                    }
                    if (clv[root1Text] != undefined && clv[root1Text] != null && clv[root1Text] != "") {
                        $(root1).html(html + clv[root1Text]);
                    }
                } else {
                    var text = $(root1).text();
                    // var root1TextJson = sessionStorage.getItem("root1Text");
                    // if (root1TextJson == undefined || root1TextJson == null || root1TextJson == "" || root1TextJson == "{}") {
                    //     root1TextJson = {}
                    // } else {
                    //     root1TextJson = JSON.parse(root1TextJson);
                    // }
                    // root1TextJson[text] = "";
                    // sessionStorage.setItem("root1Text", JSON.stringify(root1TextJson));
                    if (clv[text] != undefined && clv[text] != null && clv[text] != "") {
                        $(root1).text(clv[text]);
                    }
                }
            }
            this.forDomTypex = function (type, root1, clv) { //元素内 替换中文
                var typeName = $(root1).attr(type);
                if (typeName != undefined && typeName != null && typeName != "") {
                    if (clv[typeName] != undefined && clv[typeName] != null && clv[typeName] != "") {
                        $(root1).attr(type, clv[typeName]);
                    }
                }
            }
            this.loadingType = function (callBack) {//加载语言
                var kaBgUrlLanguageType = kaBgUrl.url("languageType");
                $.ajax({
                    url: kaBgUrlLanguageType.url,
                    type: kaBgUrlLanguageType.methodType,
                    data: {},
                    contentType: getKaContentType(kaBgUrlLanguageType.contentType),
                    dataType: getKaDataType(kaBgUrlLanguageType.contentType),
                    async: kaBgUrlLanguageType.async, //同步 false
                    loadLayer: 0,
                    beforeSend: function (xhr) {
                        var layerLoadData = {
                            skin: 'ka-loading-layer',
                            icon: 16,
                            shade: 0.01,
                            time: 500000
                        };
                        var loadLayerIndex = top.layui.layer.msg('数据加载', layerLoadData);
                        this.loadLayer = loadLayerIndex;
                    },
                    success: function (res) {
                        var loadLayerIndex = this.loadLayer;
                        if (loadLayerIndex > 0) {
                            top.layui.layer.close(loadLayerIndex);
                        }
                        var kaLanguageType = res.data;
                        localStorage.setItem("kaLanguageType", JSON.stringify(kaLanguageType));
                        typeof callBack == "function" && callBack(res)
                    },
                    error: function () {
                        var loadLayerIndex = this.loadLayer;
                        if (loadLayerIndex > 0) {
                            top.layui.layer.close(loadLayerIndex);
                        }
                    }
                });
            }
            this.loadingValue = function (type) {//加载语言对应的值
                var kaBgUrlKaLanguageValue = kaBgUrl.url("kaLanguageValue" + type);
                $.ajax({
                    url: kaBgUrlKaLanguageValue.url,
                    type: kaBgUrlKaLanguageValue.methodType,
                    data: {},
                    contentType: getKaContentType(kaBgUrlKaLanguageValue.contentType),
                    dataType: getKaDataType(kaBgUrlKaLanguageValue.contentType),
                    async: kaBgUrlKaLanguageValue.async, //同步 false
                    loadLayer: 0,
                    beforeSend: function (xhr) {
                        var layerLoadData = {
                            skin: 'ka-loading-layer',
                            icon: 16,
                            shade: 0.01,
                            time: 500000
                        };
                        var loadLayerIndex = top.layui.layer.msg('数据加载', layerLoadData);
                        this.loadLayer = loadLayerIndex;
                    },
                    success: function (re) {
                        var loadLayerIndex = this.loadLayer;
                        if (loadLayerIndex > 0) {
                            top.layui.layer.close(loadLayerIndex);
                        }
                        if (re.code == 200) {
                            var kaLanguageValue = re.data;
                            localStorage.setItem("kaLanguageValue", JSON.stringify(kaLanguageValue));
                        }
                    },
                    error: function () {
                        var loadLayerIndex = this.loadLayer;
                        if (loadLayerIndex > 0) {
                            top.layui.layer.close(loadLayerIndex);
                        }
                        localStorage.setItem("kaLanguageValue", JSON.stringify({}));
                    }
                });
            }
            this.renderingOption = function (div, callback) { //渲染登录URL
                const mutationCallback = (mutationsList) => {
                    for (let mutation of mutationsList) {
                        let type = mutation.type;
                        switch (type) {
                            case "childList":
                                this.forDomx(mutation.target);
                                break;
                            case "attributes":
                                this.forDomx(mutation.target);
                                break;
                            case "subtree":
                                this.forDomx(mutation.target);
                                break;
                            default:
                                break;
                        }
                    }
                };
                let config = {
                    attributes: true, //目标节点的属性变化
                    childList: true, //目标节点的子节点的新增和删除
                    characterData: true, //如果目标节点为characterData节点(一种抽象接口,具体可以为文本节点,注释节点,以及处理指令节点)时,也要观察该节点的文本内容是否发生变化
                    subtree: true, //目标节点所有后代节点的attributes、childList、characterData变化
                };
                var observe = new MutationObserver(mutationCallback);
                observe.observe(document, config);// 后面介绍config的配置
                this.loadingType(callback);
            }
        };
        exports('kaLanguage', kaLanguage);
    }
)